﻿using Microsoft.EntityFrameworkCore;
using PHMEE.Entities.DaqInfo.TdDevices;
using PHMEE.EntityFrameworkCore;
using PHMEE.IRepositories.DaqInfo.TdDevices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;

namespace PHMEE.Repositories.DaqInfo.TdDevices
{
    /// <summary>
    /// TD数据库仓储
    /// </summary>
    public class TdDataBaseRepository : EfCoreRepository<PHMEEDbContext, TdDatabase, Guid>, ITdDataBaseRepository
    {
        public TdDataBaseRepository(IDbContextProvider<PHMEEDbContext> dbContextProvider) : base(dbContextProvider)
        {

        }
        /// <summary>
        /// 获取指定的指定的数据库->指定的超级表->有哪些子表
        /// </summary>
        /// <param name="dbName"></param>
        /// <param name="stName"></param>
        /// <returns>正序返回所有子表</returns>
        public async Task<List<string>> GetSubTablesAsync(string dbName, string stName)
        {
            var dbContext = await GetDbContextAsync();
            var subTables = await dbContext.TdDeviceInstances
                .Where(instance => instance.TdDeviceType.Name == stName && instance.TdDeviceType.Database.Name == dbName)
                .OrderBy(instance => instance.InstanceName)
                .Select(instance => instance.InstanceName)
                .ToListAsync();

            return subTables;
        }
    }
}
